<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
//tao function ket noi
include('clsketnoi.php');

function taomaphieumuon() 
{ 
    return taomatudong("select mapm from tblphieumuon","mapm", 2, 14);
} 
//them du lieu
function themphieumuon()
{
    $mapm = $_POST['maphieumuon'];
    $manv = $_POST['nhanvienlap']; //function hienthinhanvien value = manv
    $tiencoc = $_POST['tongtiencoc'];
    $madg = $_POST['docgia'];
    $ngaymuon = $_POST['ngaymuon'];
    $soluong = $_POST['soluong'];
    
    $query = mysql_query("insert into tblphieumuon (mapm, manv, madg, soluong, tiencoc, ngaymuon) values ".
                                                        " ('$mapm','$manv','$madg','$soluong','$tiencoc', '$ngaymuon')",dbketnoi());
    mysql_free_result($query);
}
//xoa 
function xoaphieumuon()
{
    $mapm = $_POST['maphieumuon'];
    $query = mysql_query("SELECT tblphieumuon.mapm as mapm, mactmt FROM tblphieumuon, tblchitietphieumuontra ".
    " where tblphieumuon.mapm=tblchitietphieumuontra.mapm",dbketnoi());
    $i=0;
    while($result = mysql_fetch_array($query))
    {
        if($result['mapm']==$mapm)
        {
            $q= mysql_query("delete from tblchitietphieumuontra where mactmt='".$result['mactmt']."'", dbketnoi());
            mysql_free_result($q);
        }
        $i++;
    }
    $q = mysql_query("delete from tblphieumuon where mapm='$mapm'",dbketnoi());
    mysql_free_result($q);
  
}
//update 
function updatephieumuon()
{
    $mapm = $_POST['maphieumuon'];
    $manv = $_POST['nhanvienlap']; //function hienthinhanvien value = manv
    $tiencoc = $_POST['tongtiencoc'];
    $madg = $_POST['docgia'];
    $ngaymuon = $_POST['ngaymuon'];
    $soluong = $_POST['soluong'];
    
    $query = mysql_query("update tblphieumuon  set  manv='$manv', madg='$madg', soluong='$soluong', tiencoc='$tiencoc', ngaymuon='$ngaymuon' ".
     " where mapm='$mapm'",dbketnoi());
    mysql_free_result($query);
}

// hien thi du lieu
function hienthiphieumuon()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT mapm, soluong, tblnhanvien.hoten as tennhanvien, tbldocgia.hoten as tendocgia, ngaymuon, tiencoc ".
    " from tblphieumuon, tblnhanvien, tbldocgia where tblphieumuon.madg=tbldocgia.madg and tblphieumuon.manv=tblnhanvien.manv",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodienphieumuon.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo '<td><a href="giaodienchitietphieumuontra.php?mapm='.$result['mapm'].'">'.$result['mapm'].'</a></td>';
            echo "<td>".$result['soluong']."</td>";
            echo "<td>".$result['tendocgia']."</td>";
            echo "<td>".$result['tennhanvien']."</td>";
            echo "<td>".$result['tiencoc']."</td>";
            echo "<td>".$result['ngaymuon']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã PM ","Số Lượng","Của Độc Giả","Nhân Viên Lập", "Tiền Cọc", "Ngày Mượn");
    echo "<table style='border-color:black;'  cellspacing='1' width='670'>";
    echo "<tr>";
    for($i=0; $i<=5; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}

function hienthinhanvien()
{
    $query = mysql_query("select manv, hoten, bophan from tblnhanvien where bophan='Thủ kho'",dbketnoi());
    $i=0;
    echo "<select size='1' name='nhanvienlap'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['manv']."'>".$result['hoten']."</option>";
        $i++;
    }
    echo "</select>";
}



?>
